﻿using System.Linq;

namespace NorthwindEntities
{
    class CustomerManipulator
    {
        public static void AddCustomer(string id, string companyName, string contactName, 
            string contactTitle, string addres, string city, string region, string postalCode,
            string country, string phone, string fax) 
        {
            NorthwindEntities context = new NorthwindEntities();
            Customer newCustomer = new Customer() 
            {
                CustomerID = id,
                CompanyName = companyName,
                ContactName = contactName,
                ContactTitle = contactTitle,
                Address = addres,
                City = city,
                Region = region,
                PostalCode = postalCode,
                Country = country,
                Phone = phone,
                Fax = fax
            };
            context.Customers.AddObject(newCustomer);
            context.SaveChanges();
        }

        public static void ModifyCustomer(string id, string city) 
        {
            NorthwindEntities context = new NorthwindEntities();

            Customer customerToModify = GetCustomerById(id, context);
            customerToModify.City = city;
            context.SaveChanges();
        }
  
        private static Customer GetCustomerById(string id, NorthwindEntities context)
        {
            Customer customerToModify = context.Customers.FirstOrDefault(c => c.CustomerID == id);
            return customerToModify;
        }

        public static void DeleteProduct(string id) 
        {
            NorthwindEntities context = new NorthwindEntities();
            Customer customerToDelete = GetCustomerById(id, context);
            context.DeleteObject(customerToDelete);
            context.SaveChanges();
        }
    }
}
